import { createRouter,createWebHistory } from "vue-router";
import leftBar from '../components/LeftBar'
// 定义路由规则
const routes = [
    {
        path: '/',
        components: {
            left_bar: leftBar,
            right_content: () => import('../pages/DataPage.vue'),
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
    { // 邮箱 / 密码登录页面
        path: '/login', 
        components: {
            all_view:() => import('../pages/LoginPage')
        },
        meta : {
            showView: 'allView', // 指定结构路由显示
        }
    },
    { // 邮箱 / 验证码登录页面
        path: '/mailCheck',
        components: {
            all_view:() => import('../pages/MailCheck')
        },
        meta : {
            showView: 'allView', // 指定结构路由显示
        }
    },
    { // 全屏取餐码页面
        path: '/AllPickupPage',
        components: {
            all_view: () => import('../pages/PickupPage.vue')
        },
        meta: {
            showView: 'allView', // 指定结构路由显示
        }
    },
    { // 管理员管理页面
        path: '/admin',
        components: {
            left_bar:leftBar,
            right_content: () => import('../pages/AdminPage'),
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
    { // 商品列表页面
        path: '/goodsList',
        components: {
            left_bar: leftBar,
            right_content: () => import('../pages/GoodsListPage')
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
    { // 商品类别页面
        path: '/goodsType',
        components: {
            left_bar: leftBar,
            right_content: () => import('../pages/GoodsTypePage')
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
    { // 订单页面
        path: '/orderList',
        components: {
            left_bar: leftBar,
            right_content: () => import('../pages/OrderPage.vue')
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
    { // 商铺管理页面
        path: '/shop',
        components: {
            left_bar: leftBar,
            right_content: () => import('../pages/ShopPage.vue')
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
    { // 用户管理页面
        path: '/user',
        components: {
            left_bar: leftBar,
            right_content: () => import('../pages/UserPage.vue')
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
    { // 取餐码页面
        path: '/pickupPage',
        components: {
            left_bar: leftBar,
            right_content: () => import('../pages/PickupPage.vue')
        },
        meta: {
            showView: 'showLRView', // 指定结构路由显示
        }
    },
]
const router = createRouter({
    history:createWebHistory(),
    routes
})
import Cookies from 'js-cookie' // 获取cookie的库
// 配置全局路由守卫，查看是否登录
router.beforeEach((to,form,next) => {   
    if(to.path == '/login' || to.path == '/mailCheck') { // 若为登录页面，放行
        return next()
    }
    let token = Cookies.get('token') // 获取token
    if( token == null || token == '') { // token为空拦截
        return next('/login')
    }
    return next()
})
// 路由对象暴露
export default router